﻿using System;
using System.Windows;
using CefSharp.Handler;
using NLog;
using Application = System.Windows.Forms.Application;

namespace CefWebrtcDemo.wpf
{
    public partial class DemoWindow : Window
    {
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public DemoWindow()
        {
            InitializeComponent();

            Browser.KeyboardHandler = new CefKeyBoardHandler();
            Browser.JsDialogHandler = new JsDialogHandler();
            Browser.PermissionHandler = new MyPermissionHandler();//cef setting加入use-fake-ui-for-media-stream后，导致失效

            _logger.Debug("> On DemoWindow ready...-----------------------------------------------------------");
            //test code
            RoomNumber.Text = "10128";
            DisplayName.Text = "cef_test";
            ((DemoWindowViewModel)DataContext).IsBrowserVisible = true;
            Browser.LoadUrl(Application.StartupPath + "\\wwwroot\\TestCaptureScreen.html");
            // Browser.LoadUrl("https://webrtc.github.io/samples/src/content/getusermedia/getdisplaymedia/");
            // Browser.LoadUrl("https://webrtc.github.io/samples");
        }

        private void ToggleBrowserVisibility_Click(object sender, RoutedEventArgs e)
        {
            var roomNumber = RoomNumber.Text;
            var displayName = DisplayName.Text;

            if (String.IsNullOrEmpty(roomNumber) || String.IsNullOrEmpty(displayName))
            {
                MessageBox.Show("房间号和参会名称不能为空！");
                return;
            }

            if (DataContext is DemoWindowViewModel viewModel)
            {
                viewModel.IsBrowserVisible = !viewModel.IsBrowserVisible;
                if (viewModel.IsBrowserVisible)
                {
                    var webrtcServer = "https://webrtc.lssvc.cn";
                    var videoServer = "dev.lssvc.cn";
                    var url = String.Format(
                        "{0}/dev/desktop/index.html?host={1}&roomKey={2}&displayName={3}&autoJoin=1&launchByCef=true",
                        webrtcServer,
                        videoServer,
                        roomNumber,
                        displayName);

                    //https://webrtc.github.io/samples/src/content/getusermedia/getdisplaymedia
                    Browser.LoadUrl(url);
                }
                else
                {
                    Browser.LoadUrl("about:blank");
                }
            }
        }
    }
}